import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class GoodsSet {
    List<Good> goodsList = new ArrayList<Good>();

    GoodsSet(){
        Good good1 = new Good();
        good1.setName("Apple");
        good1.setCount(10);
        good1.setPurchasingCost(15);
        good1.setPrice(20);
        good1.setType("大");
        good1.setManufacturer("姜凡好帅有限公司");
        good1.setNumber("J5201314");
        goodsList.add(good1);
        Good good2 = new Good();
        good2.setName("Banana");
        good2.setCount(50);
        good2.setPurchasingCost(20);
        good2.setPrice(30);
        good2.setType("特大");
        good2.setManufacturer("姜凡好帅有限公司");
        good2.setNumber("J1314520");
        goodsList.add(good2);
    }

    public void showLittle(){
        for (int i = 0; i < goodsList.size(); i++){
            System.out.print(i+1+"  ");
            goodsList.get(i).showLittle();
            System.out.println();
        }
    }
    public void addGood(){
        Scanner input = new Scanner(System.in);
        Good good = new Good();
        double price = 0, buy = 0;
        int cnt = 0;
        System.out.println("请输入商品编号");
        good.setNumber(input.next());
        System.out.println("请输入商品名称");
        good.setName(input.next());
        System.out.println("请输入生产厂家");
        good.setManufacturer(input.next());
        System.out.println("请输入生产日期");
        good.setDateOfProduced(input.next());
        System.out.println("请输入型号");
        good.setType(input.next());
        try {
            System.out.println("请输入进货价");
            buy = input.nextDouble();
            good.setPurchasingCost(buy);
            System.out.println("请输入零售价格");
            price = input.nextDouble();
            good.setPrice(price);
            System.out.println("请输入数量");
            cnt = input.nextInt();
        }catch (Exception e){
            System.out.println("输入不合法，请重新操作。");
            while (input.hasNext()) {
                if (input.hasNextLine()) {
                    input.nextLine();
                    break;
                } else {
                    input.next();
                }
            }
            addGood();
            return;
        }
        good.setCount(cnt);
        goodsList.add(good);
        System.out.println("添加商品成功！！！");
    }

    public void searchGoodsWithName(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入商品名称");
        String name = scanner.next();
        boolean flag = false;
        for(Good good : goodsList){
            if(name.equals(good.getName())){
                good.show();
                flag = true;
            }
        }
        if(!flag){
            System.out.println("不存在该商品。");
        }
        System.out.println("输入任意字符继续");
        scanner.next();
    }
    public void setGoodsWithTimeAddPos() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入生产厂家");
        String production = scanner.next();
        double minprice = 0;
        double maxprice = 0;
        try {
            System.out.println("请输入零售价格区间（最低价）");
            minprice = scanner.nextInt();
            System.out.println("请输入零售价格区间（最低价--最高价）");
            maxprice = scanner.nextInt();
        }catch (Exception e){
            System.out.println("输入不合法，请重新操作。");
            while (scanner.hasNext()) {
                if (scanner.hasNextLine()) {
                    scanner.nextLine();
                    break;
                } else {
                    scanner.next();
                }
            }
            setGoodsWithTimeAddPos();
            return;
        }
        boolean flag2 = false;
        for (Good good : goodsList) {
            if (production.equals(good.getManufacturer()) && minprice <= good.getPrice() && good.getPrice() <= maxprice) {
                good.show();
                flag2 = true;
            }
        }
        if (!flag2) {
            System.out.println("不存在该商品。");
        }
        System.out.println("输入任意字符继续");
        scanner.next();
    }
}